package cn.wp.ewallet.wallet.service.impl;

import cn.wp.common.core.dto.WpResult;
import cn.wp.ewallet.wallet.dao.*;
import cn.wp.ewallet.wallet.extension.util.EwalletWalletUtil;
import cn.wp.ewallet.wallet.model.dto.*;
import cn.wp.ewallet.wallet.model.entity.*;
import cn.wp.ewallet.wallet.service.AccountInfoService;
import cn.wp.ewallet.wallet.service.ExchangeMoneyInfoService;
import cn.wp.ewallet.wallet.service.MentionMoneyInfoService;
import cn.wp.ewallet.wallet.service.MoneyChangeService;
import cn.wp.ewallet.wallet.service.ProcRecordInfoService;
import cn.wp.ewallet.wmps.model.dto.ProdOrderParamDto;
import cn.wp.ewallet.wmps.model.dto.ProdOrderRelDto;
import cn.wp.ewallet.wmps.service.FinaProdInfoService;
import cn.wp.ewallet.wmps.service.ProdOrderRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant.*;

/**
 * @ClassName MoneyChangeServiceImpl
 * @Author ruixule
 * @Description TODO 提币业务服务
 * @Date 2020/1/11 11:34
 */
@Service
public class MoneyChangeServiceImpl implements MoneyChangeService {

    @Autowired
    private ExchangeMoneyInfoService exchangeMoneyInfoService;

    @Autowired
    private AccountInfoService accountInfoService;

    @Autowired
    private MentionMoneyInfoService mentionMoneyInfoService;

    @Autowired
    private ProdOrderRelService prodOrderRelService;

    @Autowired
    private FinaProdInfoService finaProdInfoService;

    /**
     * 该变量为了 同步锁 创建的变量，
     */
    private Object lock = new Object();


    /**
     * 转账 （操作类型 1001） SHQC的转账，对应业务的转出
     *
     * @param transferAccountParamDto
     * @return
     */
    @Override
    public WpResult transferMoney(MoneyChangeParamDto transferAccountParamDto) {
        synchronized (lock) {
            return accountInfoService.transferMoney(transferAccountParamDto);
        }
    }

    /**
     * 充值 （操作类型1002）
     *
     * @param moneyChangeParamDto
     * @return
     */
    @Override
    public WpResult rechargeMoney(MoneyChangeParamDto moneyChangeParamDto) {
        synchronized (lock) {
            return accountInfoService.rechargeMoney(moneyChangeParamDto);
        }
    }

    /**
     * 提币申请 （操作类型1004）包含2种币种SHQC 、USDT
     *
     * @param moneyChangeParamDto
     * @return
     */
    @Override
    public WpResult withdrawalMoneyApply(MoneyChangeParamDto moneyChangeParamDto) {
        synchronized (lock) {
            return mentionMoneyInfoService.applyMentionMoneyOrder(moneyChangeParamDto);
        }
    }

    /**
     * 提币处理（操作类型是1005）    SHQC 、USDT
     *
     * @param mentionMoneyDto
     * @return
     */
    @Override
    public WpResult withdrawalMoneyHandle(MentionMoneyDto mentionMoneyDto) {
        synchronized (lock) {
            return mentionMoneyInfoService.handlerMentionMoneyOrder(mentionMoneyDto);
        }
    }

    /**
     * 兑换申请逻辑 （操作类型 1006）
     * @param exchangeMoneyReqDto
     * @return
     */
    @Override
    public WpResult exchangeMoneyApply(ExchangeMoneyReqDto exchangeMoneyReqDto) {
        synchronized (lock){
            return  exchangeMoneyInfoService.addExchangeMoneyInfo(exchangeMoneyReqDto);
        }
    }

    /**
     * 兑换处理 （操作类型1007）
     * @param exchangeMoneyDto
     * @return
     */
    @Transactional
    @Override
    public WpResult exchangeMoneyHandler(ExchangeMoneyDto exchangeMoneyDto) {
        synchronized (lock) {
            return  exchangeMoneyInfoService.handleExchangeMoneyOrder(exchangeMoneyDto);
        }
    }



    /**
     * 定期购买   参数可自定义加入
     *
     * @return
     */
    @Override
    public WpResult buyFinancial(String finaProdId, BigDecimal purchAmount,String isAutoRenewal,String moneyTypeId) {
        synchronized (lock) {
            prodOrderRelService.purchaseFinaProd(finaProdId, purchAmount, isAutoRenewal, moneyTypeId);
        }
        return null;
    }


    /**
     * 从SHQC转入天天盈账户
     * @param money
     * @return
     */
    @Override
    public WpResult tiantianyingTransferInto(BigDecimal money) {
        synchronized (lock){
            return finaProdInfoService.turnInTianTianYingOrder (money);
        }
    }

    /**
     * 从天天盈账户转出
     * @param money
     * @return
     */
    @Override
    public WpResult tiantianyingTransferOut(BigDecimal money) {
        synchronized (lock){
            return finaProdInfoService.turnOutTianTianYingOrder (money);
        }
    }



    /**
     * 金钱赎回    参数可自定义加入
     *
     * @return
     */
    @Override
    public WpResult redemptionMoney(String prodOrderId, String finaProdId ) {
        synchronized (lock) {
            //提前赎回
            return   prodOrderRelService.earlRedemption(prodOrderId,finaProdId);
        }

    }
}
